package com.ruoyi.web.controller.system;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.QiniuCloudUtil;
import com.ruoyi.system.domain.MaterialThree;
import com.ruoyi.system.domain.MaterialTwo;
import com.ruoyi.system.domain.PickCondition;
import com.ruoyi.system.service.IWatehouseMaterialService;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 材料Controller
 * 
 * @author ruoyi
 * @date 2020-12-16
 */
@RestController
@RequestMapping("/system/watehousematerial")
public class WatehouseMaterialController extends BaseController
{

    @Resource
    private IWatehouseMaterialService IWatehouseMaterialService;
    /**
     * 修改材料
     */
    @GetMapping("/edit/{mId}")
    public AjaxResult edit(@PathVariable("mId") Long mId)
    {
        MaterialTwo  material = IWatehouseMaterialService.selectaterialByid(mId);
        return AjaxResult.success(material);
    }
    /**
     * 修改材料
     */
    @GetMapping("/material/list")
    public List queryMaterialList(MaterialThree MaterialThree)
    {
        List<MaterialThree> materialThrees = IWatehouseMaterialService.queryMaterialList(MaterialThree);
        return materialThrees;
    }

    /**
     * 修改保存仓库
     * 修改保存仓库
     */

    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody MaterialTwo  material, Long wid)
    {int a=IWatehouseMaterialService.updateMaterial(material,wid);
        if(a==2){
            return success();
        }else if(a==1){
            return error("该材料已存在");
        }else{
            return error();
        }
    }

    //@RequiresPermissions("system:material:add")
    @Log(title = "材料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody MaterialThree material, long wid)
    {
        int i = IWatehouseMaterialService.insertMaterial(material, wid);
        if (i==200){
            return  error("已有材料，添加失败");
        }else {
            return success("添加成功");
        }
    }
    //@RequiresPertem:material:remove")
    @Log(title = "材料", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult addSave(Long wid, Long mid)
    {
        if (IWatehouseMaterialService.deleteMaterialById(wid,mid)>0){
            return success();
        }else {
            return error("删除材料失败，当前材料有库存");
        }
    }

    /**
     * 查询材料列表
     */
    @PostMapping("/listAll")
    public TableDataInfo listQuery(MaterialTwo material, Integer wid)
    {
        startPage();
        List<MaterialTwo> list = IWatehouseMaterialService.selectMaterialList(material,wid);
        getDataTable(list).setCode(0);
        return getDataTable(list);
    }
    /**
     * 查询材料列表
     */
    @PostMapping("/list")
    public TableDataInfo selectWarehousematerialBywid(MaterialTwo material, @Param("wid") Integer wid)
    {
        if(wid==null)wid=0;
        List<MaterialTwo> list = IWatehouseMaterialService.selectWarehousematerialBywid(material,wid);
        return getDataTable(list);
    }/**
     * 查询材料列表
     */
    @PostMapping("/materialListall")
    public TableDataInfo selectMaterialListall(MaterialTwo material)
    {
        List<MaterialTwo> list = IWatehouseMaterialService.selectMaterialListall(material);
        return getDataTable(list);
    }

    /**
     *返回一般计量单位
     */
    @GetMapping("/reunits")
    public TableDataInfo reunitis(){
        return getDataTable(IWatehouseMaterialService.reunits());
    }



    /**
     *返回项目名字
     */
    @GetMapping("/reproname")
    public TableDataInfo reproname(){

        return getDataTable(IWatehouseMaterialService.reproname());
    }


    /**
     *出入库回显
     */
    @PostMapping("/watehouse")
    public TableDataInfo wareHouse(PickCondition pickCondition, Long mid, Long wid){
        //告诉我是哪一个仓库 哪一种材料
        HashMap<String,Object> map=new HashMap();
        map.put("mId",mid);
        map.put("id",wid);
        map.put("pickCondition",pickCondition);
        List<PickCondition> pickConditionList = IWatehouseMaterialService.selectMatById(map);
        return getDataTable(pickConditionList);
    };

    /**
     *入库
     */
    @PostMapping("/watehousing")
    public AjaxResult watehousing(@RequestBody List<PickCondition> list){
        boolean reslut=true;
        for (int i = 0; i < list.size(); i++) {
            PickCondition pickCondition = list.get(i);

            boolean pick = IWatehouseMaterialService.watehousing(pickCondition);
            reslut=reslut&&pick;
        }
        if (reslut){
            return success("入库成功");
        }else {
            return error("入库失败");
        }
    }

    /**
     *出库
     */
    @PostMapping("/outwatehouse")
    public AjaxResult outwatehouse(@RequestBody List<PickCondition> list) throws Exception {
        boolean reslut=true;
        for (int i = 0; i < list.size(); i++) {
            PickCondition pickCondition = list.get(i);
            boolean pick = IWatehouseMaterialService.outWatehouse(pickCondition);
            reslut=reslut&&pick;
        }

        if (reslut){
            return success("出库成功");
        }else {
            return error("出库失败");
        }
    }

    @PostMapping("/uplodAfor")
    public void uplodAfor(Date date,HttpServletResponse response) throws IOException {
        long time = date.getTime();
        Date date1 = new Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
        String format = simpleDateFormat.format(date1);
        Workbook workbook = IWatehouseMaterialService.exportMoreSheetByTemplate(format);
        workbook.write(response.getOutputStream());
        workbook.close();
    }
    @PostMapping("/uplodBfor" )
    public void uplodBfor(Date date,HttpServletResponse response) throws IOException, ParseException {
        long time = date.getTime();
        Date date1 = new Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
        String format = simpleDateFormat.format(date1);
        Workbook workbook = IWatehouseMaterialService.exportByTemplate(format);
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @PostMapping("/uplod" )
    public void uplod(HttpServletResponse response) throws IOException, ParseException {
        IWatehouseMaterialService.upload();
    }
}
